<?php

namespace App\Http\Controllers\Admin;

use App\Model\Permission;
use App\Model\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $rolelist = Role::get();
        return view('admin.role.list')->with(['rolelist' => $rolelist]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.role.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->input('role_name');
        //验证角色是否已经存在
        if (Role::where('role_name', $input)->first()) {
            return ['status' => 1, 'message' => '角色已经存在'];
        }

        $res = Role::create(['role_name' => $input]);
        if ($res) {
            $data = ['status' => 0, 'message' => '添加成功'];
        } else {
            $data = ['status' => 1, 'message' => '添加失败'];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = Role::find($id);

        return view('admin.role.edit', compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->all();
        $role = Role::find($input['rid']);

        $role->role_name = $input['role_name'];
        $res = $role->save();

        if ($res) {
            $data = [
                'status' => 0,
                'message' => '修改成功'
            ];
        } else {
            $data = [
                'status' => 1,
                'message' => '修改失败'
            ];
        }
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = Role::where('id', $id)->delete();
        if ($res) {
            $data = [
                'status' => 0,
                'message' => '删除成功'
            ];
        } else {
            $data = [
                'status' => 1,
                'message' => '删除失败'
            ];
        }
        return $data;
    }


    //展示授权页面
    public function Auth($id)
    {
        //获取所有权限
        $pers = Permission::get();
        //获取当前角色
        $role = Role::find($id);
        //获取当前角色拥有的权限
        $self = $role->permission;
        $self_id = [];
        //获取当前权限id
        foreach ($self as $v) {
            $self_id[] = $v->id;
        }

        return view('admin.role.auth', compact('pers', 'role', 'self_id'));
    }

    //处理授权
    public function doAuth(Request $request)
    {
        $input = $request->except('_token');
//        dd($input);

        //删除当前角色已有的权限
        \DB::table('role_permission')->where('role_id', $input['id'])->delete();

        //添加新授予的权限
        if (!empty($input['permission_id'])) {
            foreach ($input['permission_id'] as $v) {
                $res = \DB::table('role_permission')->insert(['role_id' => $input['id'], 'permission_id' => $v]);
            }
        }
        if ($res) {
            $data = [
                'status' => 0,
                'message' => '权限修改成功'
            ];
        } else {
            $data = [
                'status' => 1,
                'message' => '权限修改失败'
            ];
        }
        return $data;

    }

    //删除所有选中角色
    public function delAll(Request $request)
    {
        $input = $request->input('ids');

        $res = Role::destroy($input);
        if ($res) {
            $data = ['status' => 0, 'message' => '删除成功'];
        } else {
            $data = ['status' => 1, 'message' => '删除失败'];
        }

        return $data;
    }
}
